﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Module;
using System.Data.SqlClient;
using Qmaker.DAL;
using System.Data;

namespace DLL
{
    public class DNews
    {
        #region SQL语句

        #region 查询

        private const string SQL_SELECT = "SELECT NewsId,NewsAuthor,NewsBody,NewsCategory,NewsDescription,NewsPubDate,NewsTitle,NewsType,NewsUrl,DateCreated FROM News order by DateCreated desc";

        private const string SQL_SELECT_NEWSTYPE = "SELECT NewsId,NewsAuthor,NewsBody,NewsCategory,NewsDescription,NewsPubDate,NewsTitle,NewsType,NewsUrl,DateCreated FROM News WHERE NewsType=@NewsType order by DateCreated desc";

        #endregion

        #endregion

        #region 公共接口

        /// <summary>
        /// 获取所有可用的新闻
        /// </summary>
        /// <returns></returns>
        public List<NewsModel> GetAllNews()
        {
            List<NewsModel> result = new List<NewsModel>();

            NewsModel news = null;
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, SQL_SELECT, null))
            {
                while (reader.Read())
                {
                    news = GetInstanceByDataReader(reader);
                    if (news != null)
                        result.Add(news);
                }
            }
            return result;
        }

        /// <summary>
        /// 根据NewsType获取新闻
        /// </summary>
        /// <param name="newsType"></param>
        /// <returns></returns>
        public List<NewsModel> GetAllNewsByNewsType(int newsType)
        {
            List<NewsModel> result = new List<NewsModel>();
            SqlParameter[] parms = {
                    new SqlParameter("@NewsType",SqlDbType.Int)
                };
            parms[0].Value = newsType;
            NewsModel news = null;
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, CommandType.Text, SQL_SELECT_NEWSTYPE, parms))
            {
                while (reader.Read())
                {
                    news = GetInstanceByDataReader(reader);
                    if (news != null)
                        result.Add(news);
                }
            }
            return result;
        }





        #endregion

        #region 私有接口

        private NewsModel GetInstanceByDataReader(SqlDataReader reader)
        {
            NewsModel current = new NewsModel();
           //NewsId,NewsAuthor,NewsBody,NewsCategory,NewsDescription,NewsPubDate,NewsTitle,NewsType,NewsUrl,DateCreated

            current.NewsId = reader.IsDBNull(0) ? 0 : reader.GetInt64(0);
            current.NewsAuthor = reader.IsDBNull(1) ? string.Empty : reader.GetString(1);
            current.NewsBody = reader.IsDBNull(2) ? string.Empty : reader.GetString(2);
            current.NewsCategory = reader.IsDBNull(3) ? string.Empty : reader.GetString(3);
            current.NewsDescription = reader.IsDBNull(4) ? string.Empty : reader.GetString(4);
            current.NewsPubDate = reader.IsDBNull(5) ? DateTime.MinValue : reader.GetDateTime(5);
            current.NewsTitle = reader.IsDBNull(6) ? string.Empty : reader.GetString(6);
            current.NewsType = reader.IsDBNull(7) ? 0 : reader.GetInt32(7);
            current.NewsUrl = reader.IsDBNull(8) ? string.Empty : reader.GetString(8);
            current.DateCreated=reader.IsDBNull(9)?DateTime.MinValue:reader.GetDateTime(9);

            return current;
        }

        #endregion
    }
}
